package org.hgvc.webapp.ets.jsf.beans.admin;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.apache.commons.lang.StringUtils;
import org.hgvc.webapp.ets.bo.Agreement;
import org.hgvc.webapp.ets.bo.Member;
import org.hgvc.webapp.ets.bo.Membership;
import org.hgvc.webapp.ets.constant.BookingConstant;
import org.hgvc.webapp.ets.spring.service.MemberService;
import org.hgvc.webapp.ets.spring.service.MembershipService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;

public class AdminChangeMembershipBean extends AbstractBean {
	public static final String BEAN_NAME = "adminChangeMembershipBean";
	public static final String NAV_LIST = "adminChangeMembership";
	public static final String ACT_BLT_FINANCE_MEMBERSHIP_DEPOSIT="adminChangeMembership";
	public MemberService memberService;
	public MembershipService membershipService;
	public String membershipCode;
	public Member member;
	public Agreement agreement;
	public String remark; 
	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public List<SelectItem> getMemberships() throws BaseException {
		List<SelectItem> selectList = new ArrayList<SelectItem>();
		List<Membership> memberships = membershipService.getMembershipList();

		NumberFormat formatter = new DecimalFormat("#,###,###,##0.00");
		for (Membership membership : memberships) {
			SelectItem item = new SelectItem(membership.getCode(), membership.getName() + " - RM"
					+ formatter.format(membership.getMembershipFee()));
			selectList.add(item);
		}

		return selectList;
	}

	public String changeMembershipAction() {
	try{
		List<Membership> memberships = membershipService.getMembershipList();
		Membership toMembership=null;
		for(Membership membership : memberships){
			if(membership.getCode().equalsIgnoreCase(membershipCode)){
				toMembership = membership;
			}
		}
		
		memberService.savePreChangeMembership(agreement, toMembership, remark, getThisUser());
	}catch(BaseException be){
		initErrorBean(be);
		return NAV_LIST.concat("_" + getRoleName());
	} catch (Exception e) { 
		e.printStackTrace();
	}
		return AdminProspectMagmtBean.NAV_LIST.concat("_" + getRoleName());
	}

	public String getMembershipCode() {
		return membershipCode;
	}

	public void setMembershipCode(String membershipCode) {
		this.membershipCode = membershipCode;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public AdminChangeMembershipBean() {
		
	}

	public void iniBean(String memberId, Agreement agreement) throws Exception {
		member = (Member)commonDao.getObject(Member.class, memberId);
		remark="";
		membershipCode = "";
		this.agreement = agreement;
	}

	public void setMembershipService(MembershipService membershipService) {
		this.membershipService = membershipService;
	}

	public Agreement getAgreement() {
		return agreement;
	}

	public void setAgreement(Agreement agreement) {
		this.agreement = agreement;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

}
